package cn.shu.controller;

import cn.shu.bean.ChronicDisease;
import cn.shu.bean.Man;
import cn.shu.bean.SosMan;
import cn.shu.util.LocationUtils;
import cn.shu.util.Result;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import io.goeasy.GoEasy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * SOS
 */

@Controller
@RequestMapping(value = "/sos")
public class SosController {
    static final GoEasy goEasy = new GoEasy("http://rest-hangzhou.goeasy.io", "BC-c0b4462193fb40fd9911dc9db1df468b");

    //全局缓存
    private static List<SosMan> sosList = new ArrayList<SosMan>();
    private static Map<String,SosMan> sosManMap = new ConcurrentHashMap<String,SosMan>();
    private static Map<String, Object> cacheMap = new ConcurrentHashMap<String, Object>();// 缓存容器-每个大爷的所有信息

    @RequestMapping(value = "1")
    @ResponseBody
    public Result sos1() {
        //大爷1
        SosMan daye = new SosMan("2","胡大爷",new Double[]{120.202073,30.257326},"2017-10-0610: 11: 00");
        mockData(daye);
        return new Result().success();
    }

    @RequestMapping(value = "2")
    @ResponseBody
    public Result sos2() {
        //大爷2
        SosMan daye = new SosMan("1","张大爷",new Double[]{120.215368,30.267308},"2017-10-0610: 11: 00");
        mockData(daye);
        return new Result().success();
    }

    //根据人员列表获取sos三方通道信息，附近志愿者，最近的志愿者
    @RequestMapping(value = "getSOSInfo")
    @ResponseBody
    public Result getSOSInfo(String id) {
        goEasy.publish("demo_channel", JSON.toJSONString(cacheMap.get(id),SerializerFeature.DisableCircularReferenceDetect));
        return new Result().success();
    }

    //手动清空sos人员缓存列表
    @RequestMapping(value = "clearSosList")
    @ResponseBody
    public Result clearSosList() {
        sosList = new ArrayList<SosMan>();
        sosManMap = new HashMap<String, SosMan>();
        cacheMap = new ConcurrentHashMap<String, Object>();
        return new Result().success();
    }

    //慢性病分布坐标
    @RequestMapping(value = "getchronicDiseases")
    @ResponseBody
    public Result getchronicDiseases() {
        //杭州市慢性病分布
        if(cacheMap.get("chronicDiseaseList")==null) {
            //随机生成1000个慢性病对象
            List<ChronicDisease> chronicDiseases = new ArrayList<ChronicDisease>();
            List<Double[]> list = LocationUtils.randomLonLat(new Double[]{120.189641,30.250338}
                    ,10000,50000);
            for (Double[] doubles : list) {
                chronicDiseases.add(new ChronicDisease(doubles));
            }
            cacheMap.put("chronicDiseaseList",chronicDiseases);
        }
        return new Result().success(cacheMap.get("chronicDiseaseList"));
    }

    //模拟数据
    public static void mockData(SosMan daye) {
        List<Double[]> list = LocationUtils.randomLonLat(daye.getCoords(), 6, 1000);
        Map map = new HashMap();
        map.put("targetMan", daye);
        map.put("volunteerCoordsList", list);//附近的6个志愿者
//        map.put("nearestVolunteer",);

        //SOS通道信息
        Map sosMap = new HashMap();
        Man socialWorker = new Man("社工",
                LocationUtils.randomLonLat(daye.getCoords(), 1, 1000).get(0),
                "socialWorker");

        Man relatives = new Man("亲属",
                LocationUtils.randomLonLat(daye.getCoords(), 1, 1000).get(0),
                "relatives");

        Man doctor = new Man("医生",
                LocationUtils.randomLonLat(daye.getCoords(), 1, 1000).get(0),
                "doctor");

        Man hospital = new Man("医院",
                LocationUtils.randomLonLat(daye.getCoords(), 1, 1000).get(0),
                "hospital");

        sosMap.put("socialWorker", socialWorker);
        sosMap.put("relatives", relatives);
        sosMap.put("doctor", doctor);
        sosMap.put("hospital", hospital);

        map.put("sosMap", sosMap);
        System.out.println(JSON.toJSONString(map));

        if (!sosList.contains(daye)) {
            sosList.add(daye);
            sosManMap.put(daye.getId(),daye);//缓存
        }

        cacheMap.put(daye.getId(),map);//缓存
        map.put("sosList", sosList);
        goEasy.publish("demo_channel", JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect));
    }
}

